<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Testing for Existence [Universal Feed Parser]</title>
<link rel="stylesheet" href="feedparser.css" type="text/css">
<link rev="made" href="mailto:mark@diveintomark.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.65.1">
<meta name="keywords" content="RSS, Atom, CDF, XML, feed, parser, Python">
<link rel="start" href="index.html" title="Documentation">
<link rel="up" href="basic.html" title="Basic Features">
<link rel="prev" href="uncommon-atom.html" title="Uncommon Atom Elements">
<link rel="next" href="advanced.html" title="Advanced Features">
</head>
<body id="feedparser-org" class="docs">
<div class="z" id="intro"><div class="sectionInner"><div class="sectionInner2">
<div class="s" id="pageHeader">
<h1><a href="/"><span>Universal Feed Parser</span></a></h1>
<p><span>Parse RSS and Atom feeds in Python.  3000 unit tests.  Open source.</span></p>
</div>
<div class="s" id="quickSummary"><ul>
<li class="li1">
<a href="http://sourceforge.net/projects/feedparser/"><span>Download</span></a> ·</li>
<li class="li2">
<a href="http://feedparser.org/docs/"><span>Documentation</span></a> ·</li>
<li class="li3">
<a href="http://feedparser.org/tests/"><span>Unit tests</span></a> ·</li>
<li class="li4"><a href="http://sourceforge.net/tracker/?func=browse&amp;group_id=112328&amp;atid=661937"><span>Report a bug</span></a></li>
</ul></div>
</div></div></div>
<div id="main"><div id="mainInner">
<p id="breadcrumb">You are here: <a href="index.html">Documentation</a> → <a href="basic.html">Basic Features</a> → <span class="thispage">Testing for Existence</span></p>
<div class="section" lang="en">
<div class="titlepage">
<div>
<div><h2 class="title">
<a name="basic.existence" class="skip" href="#basic.existence" title="link to this section"><img src="images/permalink.gif" alt="[link]" title="link to this section" width="8" height="9"></a> Testing for Existence</h2></div>
<div><div class="abstract">
<h3 class="title"></h3>
<p>Feeds in the real world may be missing elements, even elements that are required by the specification.  You should always test for the existence of an element before getting its value.  Never assume an element is present.</p>
</div></div>
</div>
<div></div>
</div>
<p>Use standard <span class="application">Python</span> dictionary functions such as <tt class="function">has_key</tt> to test whether an element exists.</p>
<div class="example">
<a name="example.existence" class="skip" href="#example.existence" title="link to this example"><img src="images/permalink.gif" alt="[link]" title="link to this example" width="8" height="9"></a> <h3 class="title">Example: Testing if elements are present</h3>
<pre class="screen"><tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput"><font color='navy'><b>import</b></font> feedparser</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">d = feedparser.parse('<a href="http://feedparser.org/docs/examples/atom10.xml">http://feedparser.org/docs/examples/atom10.xml</a>')</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">d.feed.has_key(<font color='olive'>'title'</font>)</span>
<span class="computeroutput">True</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">d.feed.has_key(<font color='olive'>'ttl'</font>)</span>
<span class="computeroutput">False</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">d.feed.get(<font color='olive'>'title'</font>, <font color='olive'>'No title'</font>)</span>
<span class="computeroutput">u'Sample feed'</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">d.feed.get(<font color='olive'>'ttl'</font>, 60)</span>
<span class="computeroutput">60</span></pre>
</div>
</div>
<div style="float: left">← <a class="NavigationArrow" href="uncommon-atom.html">Uncommon Atom Elements</a>
</div>
<div style="text-align: right">
<a class="NavigationArrow" href="advanced.html">Advanced Features</a> →</div>
<hr style="clear:both">
<div class="footer"><p class="copyright">Copyright © 2004, 2005, 2006 Mark Pilgrim</p></div>
</div></div>
</body>
</html>
